package com.zhugang.week01;

/**
 * @program algorithms
 * @description: twoSum
 * @author: chanzhugang
 * @create: 2022/03/13 23:57
 */
public class TwoSum {

    public static void main(String[] args) {
        TwoSum twoSum = new TwoSum();
        int[] nums = new int[]{2, 7, 11, 15};
        int target = 13;
        int[] result = twoSum.twoSum(nums, target);
        for (int i : result) {
            System.out.println("index:" + i);
        }

    }

    /**
     * 两数之和
     * 两层for循环，暴力求解，还有更好的方法，后续讲
     *
     * @param nums   [2,7,11,15]
     * @param target 9
     * @return
     */
    public int[] twoSum(int[] nums, int target) {
        for (int i = 0; i < nums.length; i++) {
            // j = i + 1 （保证不重复）
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] + nums[j] == target) {
                    return new int[]{i, j};
                }
            }
        }
        return null;
    }



}